<?php

/**
 * @author ntourne
 * Manejo del objeto TopicAPI
 */

class Topic extends Entity {

	var $topicVO;
	
	function __construct($topicVO = null) {
		parent::__construct();
		$this->topicVO = $topicVO;
	} 
	
	
	function setVO($topicVO) {
		$this->topicVO = $topicVO;
	}

	
	/**
	 * Validate input data for searching
	 * @return true if input data for searching validates
	 */
	function validateSearch() {

		// validate firstname and lastname		
		if ( Validation::isEmptyString($this->name) && Validation::isEmptyString($this->name) ) {
			// $this->addMessage(Message::$FIRSTNAME_LASTNAME_CANT_EMPTY); // array("type" => MessageType::ERROR, "text" => "You need to input First name or Last name to run a search");
		}
			
		return Validation::isEmptyArray($this->messages);
	}
	
	
	/**
	 * Validate input data for create a new person
	 * @return true if input data for create person validates
	 */
	function validateCreate() {

		if (Validation::isEmptyString($this->name))
			$this->addMessage(Message::$TOPIC_NAME_CANT_EMPTY);
		else if (!Validation::isLessMaxLengthString($this->name, 100))
			$this->addMessage(Message::$TOPIC_NAME_LENGTH_LARGER_100_CHARS);			
		else if (!Validation::isMoreLessLengthString($this->name, 3))
			$this->addMessage(Message::$TOPIC_NAME_LENGTH_SMALLER_3_CHARS);

		return Validation::isEmptyArray($this->messages);
	}
	
	
	/**
	 * Return a Topic by id
	 * @return TopicVO
	 */
	function getTopicById($method = NULL) {

		$db = new DBManager();
		$conn = $db->getConnection();
		$dbUtil = new DBUtil();

		$dbUtil->setSPName("SEL_TopicById");
		$dbUtil->setParameter($this->topicVO->id, ParameterDB::INT_TYPE);
		$sent = $dbUtil->generateSP();
		$sql = mysql_query($sent, $conn) or die (mysql_error());
		
		$row = mysql_fetch_array($sql);
		$db->closeConnection();
		
		if ($row == null)
			return false;
		
		$topicVO = $this->populateTopicVO($row);
		
		if ($method == "full") {
			
			$commentVO = new CommentVO();
			$commentVO->topic_id = $this->topicVO->id;
			$topicCommentVO = Comment::getCommentsByTopicId($commentVO);
			
			$topicVO->comments = $topicCommentVO;
		}		
		
		return $topicVO;
	}
	
	
	
	/**
	 * Return list of topics ordered by creation date
	 * @return List<TopicVO>
	 */
	function getTopicsByCreationDate() {

		$db = new DBManager();
		$conn = $db->getConnection();
		$dbUtil = new DBUtil();

		$dbUtil->setSPName("SEL_TopicsByCreationDate");
		$sent = $dbUtil->generateSP();
		$sql = mysql_query($sent, $conn) or die (mysql_error());
		
		$topics = array();
		while ($row = mysql_fetch_array($sql)) {
			$topics[] = $this->populateTopicVO($row);
		}
				
		$db->closeConnection();
		
		return $topics;
	}
	
	
	/**
	 * Return list of topics ordered by username owner
	 * @return List<TopicVO>
	 */
	function getTopicsByOwner() {

		$db = new DBManager();
		$conn = $db->getConnection();
		$dbUtil = new DBUtil();

		$dbUtil->setSPName("SEL_TopicsByOwner");
		$dbUtil->setParameter($this->topicVO->user_username, ParameterDB::VARCHAR_TYPE);
		$sent = $dbUtil->generateSP();
		
		$sql = mysql_query($sent, $conn) or die (mysql_error());
		
		$topics = array();
		while ($row = mysql_fetch_array($sql)) {
			$topics[] = $this->populateTopicVO($row);
		}
				
		$db->closeConnection();
		
		return $topics;
	}	
	
	

	
	/**
	 * Return list of Topics searching by name
	 * @return List<TopicVO>
	 */	
	function getTopicsByNameSearch() {
		
		$db = new DBManager();
		$conn = $db->getConnection();
		$dbUtil = new DBUtil();

		$dbUtil->setSPName("SEL_TopicsByNameSearch");
		$dbUtil->setParameter($this->topicVO->name, ParameterDB::VARCHAR_TYPE);
		$sent = $dbUtil->generateSP();
		
		$sql = mysql_query($sent, $conn) or die (mysql_error());
		
		$topics = array();
		while ($row = mysql_fetch_array($sql)) {
			$topics[] = $this->populateTopicVO($row);
		}
				
		$db->closeConnection();
		
		return $topics;		
		
	}
	
	
	function populateTopicVO($row) {
		$topicVO = new TopicVO();
		$topicVO->id = $row['id'];
		$topicVO->name = $row['name'];
		$topicVO->description = $row['description'];
		$topicVO->friendlyurl = $row['friendlyurl'];
		$topicVO->followerscount = $row['followerscount'];
		$topicVO->msgcount = $row['msgcount'];
		$topicVO->private = $row['private'];
		$topicVO->creationdate = $row['creationdate'];
		$topicVO->user_id = $row['user_id'];
		$topicVO->user_username = $row['user_username'];
		$topicVO->status = $row['status'];
		return $topicVO;
	}
	
	
	/**
	 * Dummy method
	 * @return String
	 */
	function dummy() {
		return "dummy data";
	}
}



/**
 * @author ntourne
 * Manejo del objeto TopicVO
 */
class TopicVO {
	
	var $id;
	var $name;
	var $description;
	var $friendlyurl;
	var $followerscount;
	var $msgcount;
	var $private;
	var $creationdate;
	var $user_id;
	var $user_username;
	var $status;
	var $comments;

	
	function __construct($_GET = null) {
		if ($_GET != null)
			$this->id = $_GET['id'];
	}
	
	
	// apply linkify to description
	function linkifyDescription() {
		$this->description = Utilities::linkify($this->description);
	}
	
	
	// apply linkify to all comments
	function linkifyComments() {
		$linkyfyComments = array();
		foreach ($this->comments as $commentVO) {
			$commentVO->msg = Utilities::linkify($commentVO->msg);
			$linkyfyComments[] = $commentVO;
		}
		$this->comments = $linkyfyComments;
	}

	
	function highlight($q) {
		$this->name = ereg_replace($q, "<span class='highlight'>$q</span>", $this->name);
	}

	
}